package znbkListSc.fragment;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;
import znbkListSc.entity.CollectionListEntity;
import znbkListSc.presenter.CollectionListPresenter;
import znbkListSc.adapter.CollectionListAdapter;
import com.example.lancooznbk.view.base.BaseMainFragment;
import znbkListSc.ICollectionListView;
import com.example.lancooznbk.view.widget.CustomFooterView;
import com.example.lancooznbk.view.widget.PopupMenu;
import com.example.lancooznbk.view.widget.SearchEditText;

import java.util.Map;

/**
 * Created by Administrator on 2018\2\9 0009.
 */

public class CollectionListFragment extends BaseMainFragment<CollectionListPresenter> implements ICollectionListView, View.OnClickListener {

    private SearchEditText etSearch;
    private CollectionListAdapter mAdapter;
    private TextView mTvNumber;
    private Map<String,String> mLeanTypeMap;
    private PopupMenu mPopupMenu;
    private View mMask;
    private RelativeLayout mSpLayout;

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_list_collection;
    }

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {

        mPresenter = new CollectionListPresenter(this);

        mMask = view.findViewById(R.id.bg_masker);
        mSpLayout = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);

        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.menu_scj);
        ((LinearLayout)toolBarLayout.findViewById(R.id.layout_toolbar_back)).setOnClickListener(this);


        mTvNumber = (TextView) view.findViewById(R.id.tv_paperNumber);


        etSearch = (SearchEditText) view.findViewById(R.id.et_search);
        etSearch.setOnSearchClickListener(new SearchEditText.OnSearchClickListener() {
            @Override
            public void onSearchClick(View view, String keyword) {
                mPresenter.searchFavouriteItem(etSearch.getText().toString());

            }
        });

        mAdapter = new CollectionListAdapter(getHolderActivity());
        mAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new CollectionListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mPresenter.openFavoriteItemDetail(position, mAdapter.getAdapterItemList().get(position));
            }

            @Override
            public void onItemDel(View view, int position) {
                mPresenter.delItemFavourite(position,mAdapter.getAdapterItemList().get(position));
            }
        });
    }

    @Override
    protected void updateViews(boolean isShowLoading) {
        mPresenter.getData(isShowLoading);
    }


    @Override
    public void loadData(CollectionListEntity data) {
        if (mRecyclerView.getLayoutManager() != null){
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }
        mAdapter.clearAndAdd(data.getFavourite());
        loadComplete(mAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void loadMoreData(CollectionListEntity data) {
        mAdapter.add(data.getFavourite());
        loadComplete(mAdapter.getAdapterItemCount() >= data.getTotalCount());
    }

    @Override
    public void delFavouriteSucceed() {
        mAdapter.remove();
        if (mAdapter.getAdapterItemCount() == 0){
            showNoData(getString(R.string.no_favourite_list));
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){

            case R.id.layout_toolbar_back:
                HideKeyboard();
                getHolderActivity().finish();
                break;
        }
    }

    //隐藏虚拟键盘:Fragment进行切换的时候使用
    public  void HideKeyboard() {
        /**隐藏软键盘**/
        View viewK = getHolderActivity().getWindow().peekDecorView();
        if(viewK != null){
            InputMethodManager inputmanger = (InputMethodManager) getHolderActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
            inputmanger.hideSoftInputFromWindow(viewK.getWindowToken(), 0);
        }
    }

}